 package whu.o2o;

import java.util.ArrayList;

import whu.o2o.View.LoadDialog;
import whu.o2o.util.ConstantData;
import whu.o2o.util.ImeUtil;
import whu.o2o.util.ParseXml;
import whu.o2o.util.Utils;
import whu.o2o.util.ConstantData.SharedPrefs;
import whu.o2o.vo.BaseinfoVO;
import whu.o2o.vo.PersonVO;
import android.R.integer;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;
import whu.o2o.R;
import whu.o2o.connection.CommonHttpConnect;
import whu.o2o.connection.Params;


public class LoginActivity extends BaseActivity implements OnClickListener{
	private TextView tv_register;
	private ImageButton ib_back;
	private ImageButton ib_submit;
	private EditText et_phone;
	private EditText et_password;
	private TextView tv_wjma;
	private Context mcontext;
	private String phone,password;
	private SharedPreferences sp;
	private String[] str;
	private Editor editor;
	private PersonVO baseinfovo;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.login_layout);
		mcontext = this;
		initView();
		initialComponent();
	}
	
	private void initView(){
		tv_register = (TextView) findViewById(R.id.tv_login_wyzc);
		ib_back = (ImageButton) findViewById(R.id.ib_login_back);
		ib_submit = (ImageButton) findViewById(R.id.ib_login_submit);
		et_phone = (EditText) findViewById(R.id.et_login_username);
		et_password = (EditText) findViewById(R.id.et_login_password);
		tv_wjma = (TextView) findViewById(R.id.tv_login_wjmm);
		
		baseinfovo = new PersonVO();
	
		tv_register.setOnClickListener(this);
		tv_wjma.setOnClickListener(this);
		ib_back.setOnClickListener(this);
		ib_submit.setOnClickListener(this);
	} 

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		Intent intent;

		switch (v.getId()) {
		case R.id.tv_login_wyzc:
			intent = new Intent(mcontext, RegisterActivity.class);
			startActivity(intent);
			break;

		case R.id.ib_login_back:
			this.finish();
			break;
			
		case R.id.tv_login_wjmm:
			intent = new Intent(mcontext, RetsetPasswordActivity.class);
			startActivity(intent);
			break;
			
		case R.id.ib_login_submit:
			 phone = et_phone.getText().toString().trim();
			 password = et_password.getText().toString().trim();
			if (TextUtils.isEmpty(phone) || !Utils.isMobileNO(phone)) {
				ImeUtil.showToast(this, "请正确填写手机号码",2000);
				return;
			}
			
			if (TextUtils.isEmpty(password) ) {
				ImeUtil.showToast(this, "请正确填写登录密码", 2000);
				return;
			}
			requestData();
			break;

		}
		
	}
	
	private void initialComponent(){
		sp = getSharedPreferences("info",Context.MODE_PRIVATE);
		editor=sp.edit();
		str=new String[2];
	}
	
	private void requestData() {

	}

	@Override
	protected void parseData(final String xml) {
		      LoadDialog.closeDialog();
		      Log.d("147", xml);
				if(xml.contains("xml"))
				{
				     ImeUtil.showToast(this, "登录成功", 2000);
					mApplication.getThreadPool().execute(new Runnable() {

						@Override
						public void run() {
						  
							ParseXml.getInstance().parseLoginInfoXml(xml, baseinfovo);
							mHttpHandler.sendEmptyMessage(ConstantData.Data.PARSE_XML_MSG_CODE);
						}
					});

				return;
					
				}
		switch (Utils.mystr2int(xml)) {
		case 1:
			
		
			finish();
			break;
		case 0:
			ImeUtil.showToast(this, "抱歉，账号或密码有误，请重新填写", 2000);
			break;
		case -1:
			ImeUtil.showToast(this, "用户不存在", 2000);
			break;
		case -2:
			ImeUtil.showToast(this, "登录异常", 2000);
			break;

		default:

			
			break;
		}
		
		
	}
	@Override
	protected void setData() {
		// TODO Auto-generated method stub
        
		mApplication.saveUserVO(baseinfovo);
		Intent i = new Intent(ConstantData.Action.loginTag);
		sendBroadcast(i);
		finish();
		super.setData();
	}

}
